
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ElTreeModelEvent, ElTreeModelData } from './tree'
import { ModelStandard } from './utils';

export declare type UserSafeHooks = {
  findAllChecked: () => string[];
  removeAllChecked: () => void;
  updateItemChecked: (label: string | number) => void;
  updateItemExpanded: (label: string | number) => void;
};

@Component({
  selector: 'el-tree',
  templateUrl: './tree.component.html'
})
export class TreeComponent  implements OnInit {
  // @Input() model!: ElTreeModelData[];
  @Output() modelChange: EventEmitter<ElTreeModelEvent> = new EventEmitter();
  @Input() emptyText: string ='';
  @Input() showCheckbox: boolean = false;
  @Input() defaultExpandAll: boolean = false;
  @Input() defaultExpandedKeys: Array<string | number> = [];
  @Input() defaultCheckedKeys: Array<string | number> = [];
  @Input() expandOnClickNode: boolean = true;
  @Input() indent: number = 16;
  @Input() accordion: boolean = false;
  @Input() disabled: boolean = false;
  @Input() model: ElTreeModelData[] = [];
  userSafeHooks!: () => UserSafeHooks;


  constructor() {
    var _this =  this;
    _this.userSafeHooks = function () {
      return ({
        findAllChecked: _this.findAllChecked.bind(_this),
        updateItemChecked: _this.updateChecked.bind(_this),
        updateItemExpanded: _this.updateExpanded.bind(_this),
        removeAllChecked: _this.removeAllChecked.bind(_this),
      });
    };
  }

  ngOnInit(): void {
    const standardTool = new ModelStandard({
      initDepth: 0,
      defaultExpandAll: this.defaultExpandAll,
      defaultExpandedKeys: this.defaultExpandedKeys,
      defaultCheckedKeys: this.defaultCheckedKeys,
    })
    this.model = standardTool.filterModel(this.model)
    console.log(this.model,this.model,'1111')
  }


  findAllChecked() {
    if (!this.showCheckbox)
      return [];
    return ModelStandard.FindAllChecked(this.model);
  }

  removeAllChecked(): void {
    ModelStandard.LoopRemoveChecked(this.model);
  }

  updateExpanded(label: string | number): void {
    this.model = ModelStandard.DeepUpdateExpanded(label, this.model, this.accordion);
  }

  updateChecked(label: string | number): void {
    if (!this.showCheckbox)
      return;
      console.log(label, this.model);
    this.model = ModelStandard.DeepUpdateChecked(label, this.model);
  }

  emitter(next: ElTreeModelEvent): void {
    this.modelChange.emit(next);
  }

 
}
